package com.itheima.controller;

import com.itheima.advice.RecordLogAnno;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    private DeptService deptService;
    @GetMapping
    public Result list(){
        log.info("查询部门列表");
        List<Dept> depts = deptService.findAll();
        return Result.success(depts);
    }
    @RecordLogAnno
    @DeleteMapping
    public Result delete( Integer id){
        log.info("根据id删除部门, id: {}" , id);
        deptService.deleteById(id);
        return Result.success();
    }
    @RecordLogAnno
    @PostMapping
    public Result sava(@RequestBody Dept dept){
        log.info("新增部门, dept: {}" , dept);
        deptService.sava(dept);
        return Result.success();
    }
    @RecordLogAnno
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据ID查询, id: {}" , id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }
    @RecordLogAnno
    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("修改部门, dept: {}" , dept);
        deptService.update(dept);
        return Result.success();
    }
}
